home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / BISON-TC / FILES.C < prev    next >
Text File  |  1990-01-23  |  9KB  |  384 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  25. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  26. #endif
  27.  
  28. #ifdef THINK_C
  29. #define unlink    remove
  30. #define XPFILE    "bison.simple"
  31. #define XPFILE1    "bison.hairy"
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include "system.h"
  36. #include "files.h"
  37. #include "new.h"
  38. #include "gram.h"
  39.  
  40. FILE *finput = NULL;
  41. FILE *foutput = NULL;
  42. FILE *fdefines = NULL;
  43. FILE *ftable = NULL;
  44. FILE *fattrs = NULL;
  45. FILE *fguard = NULL;
  46. FILE *faction = NULL;
  47. FILE *fparser = NULL;
  48.  
  49. /* File name specified with -o for the output file, or 0 if no -o.  */
  50. char *spec_outfile;
  51.  
  52. char *infile;
  53. char *outfile;
  54. char *defsfile;
  55. char *tabfile;
  56. char *attrsfile;
  57. char *guardfile;
  58. char *actfile;
  59. char *tmpattrsfile;
  60. char *tmptabfile;
  61.  
  62. #ifndef THINK_C
  63. extern char    *mktemp();    /* So the compiler won't complain */
  64. #endif
  65. extern char    *getenv();
  66. FILE    *tryopen();    /* This might be a good idea */
  67. void done();
  68.  
  69. extern int verboseflag;
  70. extern int definesflag;
  71. int fixed_outfiles = 0;
  72.  
  73. #ifdef THINK_C
  74. char actfile_buffer[FILENAME_MAX];
  75. char attrsfile_buffer[FILENAME_MAX];
  76. char tabfile_buffer[FILENAME_MAX];
  77. #endif
  78.  
  79.  
  80. char*
  81. stringappend(string1, end1, string2)
  82. char *string1;
  83. int end1;
  84. char *string2;
  85. {
  86.   register char *ostring;
  87.   register char *cp, *cp1;
  88.   register int i;
  89.  
  90.   cp = string2;  i = 0;
  91.   while (*cp++) i++;
  92.  
  93.   ostring = NEW2(i+end1+1, char);
  94.  
  95.   cp = ostring;
  96.   cp1 = string1;
  97.   for (i = 0; i < end1; i++)
  98.     *cp++ = *cp1++;
  99.  
  100.   cp1 = string2;
  101.   while (*cp++ = *cp1++) ;
  102.  
  103.   return ostring;
  104. }
  105.  
  106.  
  107. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  108.    the output files, and opens the tmp files and the parser */
  109. void
  110. openfiles()
  111. {
  112.   char *name_base;
  113.   register char *cp;
  114.   char *filename;
  115.   int base_length;
  116.   int short_base_length;
  117.  
  118. #ifdef VMS
  119.   char *tmp_base = "sys$scratch:b_";
  120. #else
  121.   char *tmp_base = "/tmp/b.";
  122. #endif
  123.   int tmp_len;
  124.  
  125. #ifdef MSDOS
  126.   tmp_base = getenv ("TMP");
  127.   if (tmp_base == 0)
  128.     tmp_base = "";
  129.   strlwr (infile);
  130. #endif /* MSDOS */
  131.  
  132.   tmp_len = strlen (tmp_base);
  133.  
  134.   if (spec_outfile)
  135.     {
  136.       /* -o was specified.  The precise -o name will be used for ftable.
  137.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  138.       name_base = spec_outfile;
  139. #ifdef MSDOS
  140.       strlwr (name_base);
  141. #endif /* MSDOS */
  142.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  143.       base_length = strlen (name_base);
  144.       if (!strcmp (name_base + base_length - 2, ".c"))
  145.     base_length -= 2;
  146.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  147.       short_base_length = base_length;
  148.       if (!strcmp (name_base + short_base_length - 4, ".tab"))
  149.     short_base_length -= 4;
  150.       else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  151.     short_base_length -= 4;
  152.     }
  153.   else
  154.     {
  155.       /* -o was not specified; compute output file name from input
  156.      or use y.tab.c, etc., if -y was specified.  */
  157.  
  158.       name_base = fixed_outfiles ? "y.y" : infile;
  159.  
  160.       /* Discard any directory names from the input file name
  161.      to make the base of the output.  */
  162.       cp = name_base;
  163.       while (*cp)
  164.     {
  165.       if (*cp == '/')
  166.         name_base = cp+1;
  167.       cp++;
  168.     }
  169.  
  170.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  171.  
  172.       base_length = strlen (name_base);
  173.       if (!strcmp (name_base + base_length - 2, ".y"))
  174.     base_length -= 2;
  175.       short_base_length = base_length;
  176.  
  177. #ifdef VMS
  178.       name_base = stringappend(name_base, short_base_length, "_tab");
  179. #else
  180. #ifdef MSDOS
  181.       name_base = stringappend(name_base, short_base_length, "_tab");
  182. #else
  183.       name_base = stringappend(name_base, short_base_length, ".tab");
  184. #endif /* not MSDOS */
  185. #endif
  186.       base_length = short_base_length + 4;
  187.     }
  188.  
  189.   finput = tryopen(infile, "r");
  190.  
  191.   filename = getenv("BISON_SIMPLE");
  192. #ifdef MSDOS
  193.   /* file doesn't exist in curent directory, try in INIT directory */
  194.   cp = getenv("INIT");
  195.   if (filename == 0 && cp != 0)
  196.     {
  197.       filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  198.       strcpy(filename, cp);
  199.       cp = filename + strlen(filename);
  200.       *cp++ = '/';
  201.       strcpy(cp, PFILE);
  202.     }
  203. #endif /* MSDOS */
  204.   fparser = tryopen(filename ? filename : PFILE, "r");
  205.  
  206.   if (verboseflag)
  207.     {
  208. #ifdef MSDOS
  209.       outfile = stringappend(name_base, short_base_length, ".out");
  210. #else
  211.       outfile = stringappend(name_base, short_base_length, ".output");
  212. #endif
  213.       foutput = tryopen(outfile, "w");
  214.     }
  215.  
  216.   if (definesflag)
  217.     {
  218.       defsfile = stringappend(name_base, base_length, ".h");
  219.       fdefines = tryopen(defsfile, "w");
  220.     }
  221.  
  222. #ifdef MSDOS
  223.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  224.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  225.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  226. #else
  227. #ifdef THINK_C
  228.   actfile = tmpnam (actfile_buffer);
  229.   tmpattrsfile = tmpnam (attrsfile_buffer);
  230.   tmptabfile = tmpnam (tabfile_buffer);
  231. #else
  232.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  233.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  234.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  235. #endif /* not THINK_C */
  236. #endif /* not MSDOS */
  237.  
  238.   faction = tryopen(actfile, "w+");
  239.   fattrs = tryopen(tmpattrsfile,"w+");
  240.   ftable = tryopen(tmptabfile, "w+");
  241.  
  242. #ifndef MSDOS
  243.   unlink(actfile);
  244.   unlink(tmpattrsfile);
  245.   unlink(tmptabfile);
  246. #endif
  247.  
  248.     /* These are opened by `done' or `open_extra_files', if at all */
  249.   if (spec_outfile)
  250.     tabfile = spec_outfile;
  251.   else
  252.     tabfile = stringappend(name_base, base_length, ".c");
  253.  
  254. #ifdef VMS
  255.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  256.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  257. #else
  258. #ifdef MSDOS
  259.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  260.   guardfile = stringappend(name_base, short_base_length, ".guc");
  261. #else
  262.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  263.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  264. #endif /* not MSDOS */
  265. #endif /* not VMS */
  266. }
  267.  
  268.  
  269.  
  270. /* open the output files needed only for the semantic parser.
  271. This is done when %semantic_parser is seen in the declarations section.  */
  272.  
  273. void
  274. open_extra_files()
  275. {
  276.   FILE *ftmp;
  277.   int c;
  278.   char *filename, *cp;
  279.  
  280.   fclose(fparser);
  281.  
  282.   filename = (char *) getenv ("BISON_HAIRY");
  283. #ifdef MSDOS
  284.   /* file doesn't exist in curent directory, try in INIT directory */
  285.   cp = getenv("INIT");
  286.   if (filename == 0 && cp != 0)
  287.     {
  288.       filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  289.       strcpy(filename, cp);
  290.       cp = filename + strlen(filename);
  291.       *cp++ = '/';
  292.       strcpy(cp, PFILE1);
  293.     }
  294. #endif
  295.   fparser= tryopen(filename ? filename : PFILE1, "r");
  296.  
  297.         /* JF change from inline attrs file to separate one */
  298.   ftmp = tryopen(attrsfile, "w");
  299.   rewind(fattrs);
  300.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  301.     putc(c,ftmp);
  302.   fclose(fattrs);
  303.   fattrs=ftmp;
  304.  
  305.   fguard = tryopen(guardfile, "w");
  306.  
  307. }
  308.  
  309.     /* JF to make file opening easier.  This func tries to open file
  310.        NAME with mode MODE, and prints an error message if it fails. */
  311. FILE *
  312. tryopen(name, mode)
  313. char *name;
  314. char *mode;
  315. {
  316.   FILE    *ptr;
  317.  
  318.   ptr = fopen(name, mode);
  319.   if (ptr == NULL)
  320.     {
  321.       fprintf(stderr, "bison: ");
  322.       perror(name);
  323.       done(2);
  324.     }
  325.   return ptr;
  326. }
  327.  
  328. void
  329. done(k)
  330. int k;
  331. {
  332.   if (faction)
  333.     fclose(faction);
  334.  
  335.   if (fattrs)
  336.     fclose(fattrs);
  337.  
  338.   if (fguard)
  339.     fclose(fguard);
  340.  
  341.   if (finput)
  342.     fclose(finput);
  343.  
  344.   if (fparser)
  345.     fclose(fparser);
  346.  
  347.   if (foutput)
  348.     fclose(foutput);
  349.  
  350.     /* JF write out the output file */
  351.   if (k == 0 && ftable)
  352.     {
  353.       FILE *ftmp;
  354.       register int c;
  355.  
  356.       ftmp=tryopen(tabfile, "w");
  357.       rewind(ftable);
  358.       while((c=getc(ftable)) != EOF)
  359.         putc(c,ftmp);
  360.       fclose(ftmp);
  361.       fclose(ftable);
  362.     }
  363.  
  364. #ifdef VMS
  365.   delete(actfile);
  366.   delete(tmpattrsfile);
  367.   delete(tmptabfile);
  368.   if (k==0) sys$exit(SS$_NORMAL);
  369.   sys$exit(SS$_ABORT);
  370. #else
  371. #ifdef MSDOS
  372.   if (actfile) unlink(actfile);
  373.   if (tmpattrsfile) unlink(tmpattrsfile);
  374.   if (tmptabfile) unlink(tmptabfile);
  375. #endif /* MSDOS */
  376. #ifdef THINK_C
  377.   if (actfile) unlink(actfile);
  378.   if (tmpattrsfile) unlink(tmpattrsfile);
  379.   if (tmptabfile) unlink(tmptabfile);
  380. #endif /* THINK_C */
  381.   exit(k);
  382. #endif /* not VMS */
  383. }
  384.